package cn.wolfcode.luowowo.comment.service.impl;

import cn.wolfcode.luowowo.comment.domain.StrategyComment;
import cn.wolfcode.luowowo.comment.query.StrategyCommentQuery;
import cn.wolfcode.luowowo.comment.respository.IStrategyCommentRepository;
import cn.wolfcode.luowowo.comment.service.IStrategyCommentService;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class StrategyCommentServiceImpl implements IStrategyCommentService {
    //拥有基本的crud功能和分页排序功能
    @Autowired
    private IStrategyCommentRepository repository;
    //拥有较为底层的贾琏预置事,实现mongodb操作,这里用它来完成较为复杂的操作
    @Autowired
    private MongoTemplate template;

    @Override
    public Page query(StrategyCommentQuery qo) {

        //查询条件
        Query query = new Query();
        if(qo.getDetailId() != -1){
            //攻略id条件
            query.addCriteria(Criteria.where("detailId").is(qo.getDetailId()));
        }
        //查总数
        long count = template.count(query, StrategyComment.class);

        if (count == 0){
            return Page.empty();
        }
        //参数1:当前页, 注意, 从0开始
        //参数2:每页显示条数
        //参数3:排序规则, 参数a: 表示倒序  参数b:哪个字段
        PageRequest pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(),
                Sort.by(Sort.Direction.DESC, "createTime"));
        query.with(pageable);
        //查某一页  List
        List<StrategyComment> list = template.find(query, StrategyComment.class);
        // PageImpl  Page接口实现类
        return new PageImpl(list, pageable,count );

    }

    @Override
    public void save(StrategyComment strategyComment) {
        if (!StringUtils.hasLength(strategyComment.getId())){
            strategyComment.setCreateTime(new Date());
        }
        repository.save(strategyComment);
    }

    @Override
    public void thumbUp(String toid, Long userId) {
        //获取当前操作的评论对象
        Optional<StrategyComment> op = repository.findById(toid);
        StrategyComment comment = op.get();
        //判断评论对象是否为空
        if (comment != null){
            //获取评论中点赞的用户集合
            List<Long> thumbuplist = comment.getThumbuplist();
            //判断点赞的用户id是否存在在点赞的用户集合里面
            if (thumbuplist.contains(userId)){
                //如果存在,本次为取消点赞操作
                comment.setThumbupnum(comment.getThumbupnum()-1);
                //删除点赞集合中的用户id
                thumbuplist.remove(userId);
            }else {
                //如果不存在,本次是点赞操作
                comment.setThumbupnum(comment.getThumbupnum()+1);
                //把用户id保存到点赞集合中
                thumbuplist.add(userId);
            }
            this.save(comment);
        }
    }
}
